package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.model.Goods;
import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseVo;
import com.atguigu.gmall.list.service.SearchService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author: atguigu
 * @create: 2023-03-03 10:10
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {


    /**
     * ElasticsearchDataConfiguration自动装配类中定义对象
     */
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;

    /**
     * 创建商品索引库：
     * 方式一：调用ES提供restFul API 在Kibana中执行
     * 方式二：调用ES提供SDK（底层调用restFul API）
     *
     * @return
     */
    @GetMapping("/inner/createIndex")
    public Result createGoodsIndex() {
        //1.创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //2.设置索引库映射（指定每个字段类型）
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }


    /**
     * 删除索引库
     *
     * @return
     */
    @GetMapping("/inner/deleteIndex")
    public Result deleteIndex() {
        elasticsearchRestTemplate.deleteIndex("goods");
        return Result.ok();
    }


    /**
     * 测试接口：将指定商品SKU封装商品文档对象，保存到ES索引库
     *
     * @param skuId
     * @return
     */
    @ApiOperation("测试接口：将指定商品SKU封装商品文档对象，保存到ES索引库")
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * 测试接口：将指定商品下架
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /**
     * 对指定商品分值（热门商品）进行更新
     * 分值
     *
     * @param skuId
     * @param score
     * @return
     */
    @ApiOperation("对指定商品分值（热门商品）进行更新")
    @GetMapping("/inner/incrHotScore/{skuId}/{score}")
    public Result incrHotScore(@PathVariable("skuId") Long skuId, @PathVariable("score") int score) {
        searchService.incrHotScore(skuId, score);
        return Result.ok();
    }

    /**
     * 商品检索，包含：分页检索商品信息，动态聚合过滤条件。支持多条件过滤
     * @param searchParam
     * @return
     */
    @ApiOperation("商品站内检索")
    @PostMapping({"", "/"})
    public Result search(@RequestBody SearchParam searchParam) {
        SearchResponseVo vo = searchService.search(searchParam);
        return Result.ok(vo);
    }
}
